package com.ocom.common.utils;


import com.alibaba.fastjson.JSON;

import java.util.*;

public class SignUtils {


    /*
     * 进行ASCII码从小到大排序
     * * */
    public static SortedMap<String, Object> paramSort(Object request) {
        String paramJson = JSON.toJSONString(request);
        SortedMap<String, Object> parameters = JSON.parseObject(paramJson, TreeMap.class);
        return parameters;
    }

	public static String createSign(Object request, SortedMap<String, Object> parameters, String key) {
		if (null == parameters || parameters.isEmpty()) {
			parameters = paramSort(request);
		}
		return createSign(parameters, key);
	}

    public static String createSign(Object request, SortedMap<String, Object> parameters, String keyName, String key) {
        if (null == parameters || parameters.isEmpty()) {
            parameters = paramSort(request);
        }
        return createSign(parameters, keyName, key);
    }

    public static String createSign(SortedMap<String, Object> parameters, String key) {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            //空值不传递，不参与签名组串
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                k = k.toLowerCase();
                sb.append(k + "=" + v + "&");
            }
        }
        sb = sb.append("key=" + key);
        System.out.println("加密前的参数：" + sb);
        //MD5加密,结果转换为小写字符
        String sign = MD5.MD5Encode(sb.toString(), "utf8").toLowerCase();
        System.out.println("加密后:" + sign);
        return sign;
    }

    public static String createSign(SortedMap<String, Object> parameters, String keyName, String key) {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            //空值不传递，不参与签名组串
            if (null != v && !"".equals(v) && !"sign".equals(k) && !keyName.equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb = sb.append(keyName + "=" + key);
        System.out.println("加密前的参数：" + sb);
        //MD5加密,结果转换为大写字符
        String sign = MD5.MD5Encode(sb.toString(), "utf8").toUpperCase();
        System.out.println("加密后:" + sign);
        return sign;
    }


    public static void main(String[] args) {
        SortedMap<String, Object> sortedMap = new TreeMap<String, Object>();
        sortedMap.put("comid", 23);
        sortedMap.put("nonceStr", "PwD28s45PCfsOoC70TQBWMUma31cUr5D");
        sortedMap.put("type", 1);
        sortedMap.put("param", "mBJBxgBRP6fQeTxns2KHeeUqLQqWAH2ZZJdtHxuTJpQ=");
        sortedMap.put("transferId", "Ki5wDQUgdeZABynEb8pmNzekpuO7mSg5");
        sortedMap.put("timestamp", "1658217617");
        sortedMap.put("name", "RBaDteO1XxqiAeZB8neEFQ==");
        String sign = SignUtils.createSign(sortedMap, "4D3E7C86-DAC5-4526-96C3-C60CDEC4B820");
        System.out.println(sign);
    }
}
